﻿var Login = Ext
		.extend(
				Ext.Window,
				{
					url : "include/geral/loginAction.php",
					redirectUrl : "home.php",
					labelWidth : 100,
					iconCls : "silk-lock",
					layout : "form",
					bodyStyle : "padding:10px;",
					title : "Autenticação",
					labelAlign : "left",
					closable : false,
					resizable : false,
					draggable : false,
					constrain : true,
					width : 300,
					height : 130,
					labelWidth : 45,
					loginField : {
						fieldLabel : "Usuário",
						emptyText : "Informe seu usuário"
					},
					senhaField : {
						fieldLabel : "Senha",
						emptyText : "*fakepass*"
					},
					initComponent : function() {
						this.msgTTip = new Ext.ToolTip(
								{
									anchor : "top",
									target : "txtSenha",
									width : 225,
									bodyStyle : 'background: url("lib/ext3.2.1/ux/images/alert.png") no-repeat left; padding: 3px 0px 0px 20px;',
									html : "<div><b>A Tecla Caps Lock está ativada.<b></div>",
									listeners : {
										enable : function(a) {
											a.disable()
										}
									}
								});
						Ext
								.apply(
										this,
										{
											defaults : {
												anchor : "-18"
											},
											items : [
													Ext
															.apply(
																	{
																		xtype : "textfield",
																		msgTarget : "side",
																		itemId : "txtLogin",
																		maxLength : 30,
																		maxLengthText : "Máximo 30 caracteres.",
																		vtype : "charEPonto",
																		allowBlank : false,
																		selectOnFocus : true,
																		enableKeyEvents : true,
																		listeners : {
																			scope : this,
																			keyup : this.teclaEnter
																		}
																	},
																	this.loginField),
													Ext
															.apply(
																	{
																		xtype : "textfield",
																		inputType : "password",
																		msgTarget : "side",
																		id : "txtSenha",
																		itemId : "txtSenha",
																		maxLength : 30,
																		maxLengthText : "Máximo 30 caracteres.",
																		allowBlank : false,
																		selectOnFocus : true,
																		enableKeyEvents : true,
																		toolTip : this.msgTTip,
																		listeners : {
																			scope : this,
																			keyup : this.teclaEnter,
																			keypress : {
																				fn : function(
																						b,
																						c) {
																					var a = c
																							.getCharCode();
																					if ((c.shiftKey
																							&& a >= 97 && a <= 122)
																							|| (!c.shiftKey
																									&& a >= 65 && a <= 90)) {
																						b.toolTip
																								.show()
																					} else {
																						b.toolTip
																								.hide()
																					}
																				}
																			}
																		}
																	},
																	this.senhaField) ],
											bbar : [ {
												text : "",
												id : "errorCt",
												xtype : "tbtext",
												ref : "../errorCt",
												style : {
													color : "red"
												}
											}, "->", {
												id : "btnEntrar",
												text : "Entrar",
												tooltip : "Entrar no Sistema",
												iconCls : "silk-app-go",
												scope : this,
												handler : this.entrar
											} ]
										});
						Login.superclass.initComponent.call(this)
					},
					teclaEnter : function(a, b) {
						if (b.getKey() === b.ENTER) {
							b.stopEvent();
							this.entrar()
						}
					},
					entrar : function() {
						var b = this.getComponent("txtLogin");
						var a = this.getComponent("txtSenha");
						if (!b.isValid() || !a.isValid()) {
							return false
						}
						Ext.getCmp("btnEntrar").disable();
						this.el.mask("Autenticando...");
						Ext.Ajax.request( {
							url : this.url,
							method : "POST",
							scope : this,
							params : Ext.applyIf( {
								login : b.getValue(),
								senha : SHA1(a.getValue())
							}, this.params),
							success : function(c) {
								c = Ext.decode(c.responseText);
								if (c.success) {
									this.destroy();
									window.location.href = this.redirectUrl
								} else {
									this.errorCt.setText(c.msg);
									b.focus();
									this.el.unmask();
									Ext.getCmp("btnEntrar").enable()
								}
							}
						})
					}
				});
Ext.onReady(function() {
	Ext.QuickTips.init();
	Ext.form.CompositeField.prototype.combineErrors = false;
	new Login().show();
	setTimeout(function() {
		Ext.get("Ploading").remove();
		Ext.get("Ploading-mask").fadeOut( {
			easing : "easeOut",
			duration : 0.7,
			remove : true
		})
	}, 250)
});